/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.vunguyen.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.vunguyen.model.Bomon;
import com.vunguyen.model.Canbo;
import com.vunguyen.model.Congvan;
import com.vunguyen.model.Congvanden;
import com.vunguyen.model.Giangvien;
import com.vunguyen.model.Lichlamviec;
import com.vunguyen.model.Phancongcongvanden;
import com.vunguyen.model.PhancongcongvandenBomon;
import com.vunguyen.model.PhancongcongvandenGiangvien;
import com.vunguyen.service.CongVanService;
import com.vunguyen.service.UserService;


@Controller
public class CongVanController {

    @Autowired
    private CongVanService congvanService;
    
    @Autowired
    private UserService userService;
    
	@RequestMapping(value = "/thuky", method = RequestMethod.GET)
	public String UserThuKy(Model m, HttpServletRequest request)
	{
		if(request.getSession().getAttribute("username") != null){
			int idcanbo =  (int) request.getSession().getAttribute("username");
	    	List<Congvan> list = congvanService.getCongVan();
	        m.addAttribute("listCongVan", list);
	        //hien thi ten va chuc vu ra jsp
	        m.addAttribute("chucvu", userService.getChucVuByQuyen(idcanbo));
	        m.addAttribute("tencanbo", userService.getTenCanBoById(idcanbo).getTenCanBo());
			return "thuky";
		}
		else{
			return "chuadangnhap";
		}
	}
	@RequestMapping(value = "/banchunhiem", method = RequestMethod.GET)
	public String UserBanChuNhiem(Model m, HttpServletRequest request)
	{
		if(request.getSession().getAttribute("username") != null){
			List<Congvan> list = congvanService.getCongVan();
			int idcanbo =  (int) request.getSession().getAttribute("username");
			List<Giangvien> listGiangvien = congvanService.getGiangvien(idcanbo);
			List<Bomon> listBomon = congvanService.getBoMon();
			m.addAttribute("listGiangvien", listGiangvien);
			m.addAttribute("listBomon", listBomon);
	        m.addAttribute("listCongVan", list);
	      //hien thi ten va chuc vu ra jsp
	        m.addAttribute("chucvu", userService.getChucVuByQuyen(idcanbo));
	        m.addAttribute("tencanbo", userService.getTenCanBoById(idcanbo).getTenCanBo());
			return "banchunhiem";
		}
		else{
			System.out.println(request.getSession().getAttribute("quyen"));
			return "chuadangnhap";
			
		}
	}
	@RequestMapping(value = "/phanconggiangvien", method = RequestMethod.POST)
	public String PhanCongGiangVien(Model m, HttpServletRequest request) throws ParseException
	{
		Phancongcongvanden pccvd = new Phancongcongvanden();
		int username =  (int) request.getSession().getAttribute("username");
		Canbo nguoiphancong =  userService.getTenCanBoById(username);
		pccvd.setNguoiPhanCong(nguoiphancong.getTenCanBo());
		Congvanden cvd =new Congvanden();
		cvd.setIdCongVanDen(request.getParameter("idcongvanden"));
		String today = new SimpleDateFormat("dd-MM-yyyy").format(new Date());
		pccvd.setHanCuoi(new SimpleDateFormat("dd-MM-yyyy").parse(request.getParameter("hancuoi")));
		pccvd.setNgayPhanCong(new SimpleDateFormat("dd-MM-yyyy").parse(today));
		pccvd.setCongvanden(cvd);
		congvanService.addPhanCongCongVanDen(pccvd);
		PhancongcongvandenGiangvien pccvdGV = new PhancongcongvandenGiangvien();
		Giangvien gv = new Giangvien();
		int idcanbo = Integer.parseInt(request.getParameter("idcanbo"));
		gv.setIdCanBo(idcanbo);
		pccvdGV.setGiangvien(gv);
		pccvdGV.setIdPhanCong(pccvd.getIdPhanCong());
		pccvdGV.setPhancongcongvanden(pccvd);
		congvanService.themPhancongcongvandenGiangvien(pccvdGV);
		
		return "redirect:/banchunhiem";
	}
	@RequestMapping(value = "/phancongbomon", method = RequestMethod.POST)
	public String PhanCongBoMon(Model m, HttpServletRequest request) throws ParseException
	{
		Phancongcongvanden pccvd = new Phancongcongvanden();
		int idcanbo =  (int) request.getSession().getAttribute("username");
		Canbo nguoiphancong =  userService.getTenCanBoById(idcanbo);
		pccvd.setNguoiPhanCong(nguoiphancong.getTenCanBo());
		Congvanden cvd =new Congvanden();
		cvd.setIdCongVanDen(request.getParameter("idcongvanden"));
		String today = new SimpleDateFormat("dd-MM-yyyy").format(new Date());
		pccvd.setHanCuoi(new SimpleDateFormat("dd-MM-yyyy").parse(request.getParameter("hancuoi")));
		pccvd.setNgayPhanCong(new SimpleDateFormat("dd-MM-yyyy").parse(today));
		pccvd.setCongvanden(cvd);
		congvanService.addPhanCongCongVanDen(pccvd);
		PhancongcongvandenBomon pccvdBM = new PhancongcongvandenBomon();
		int idbomon = Integer.parseInt(request.getParameter("idbomon"));
		Bomon bm = new Bomon();
		bm.setIdBoMon(idbomon);
		pccvdBM.setBomon(bm);
		pccvdBM.setPhancongcongvanden(pccvd);
		pccvdBM.setIdPhanCong(pccvd.getIdPhanCong());
		congvanService.themPhancongcongvandenBomon(pccvdBM);
		return "redirect:/banchunhiem";
	}
	@RequestMapping(value = "/capnhatphancong", method = RequestMethod.POST)
    public String updatePhanCong(@RequestParam("idphancong") int idphancong, HttpServletRequest request) throws ParseException
    {
		Phancongcongvanden pccvdnew = new Phancongcongvanden();
		pccvdnew.setHanCuoi(new SimpleDateFormat("dd-MM-yyyy").parse(request.getParameter("hancuoi")));
		if(request.getParameter("xacnhanbcn") == null)
		{
			pccvdnew.setXacNhanBcn(0);
			//System.out.println("no");
			
		}else
		{
			pccvdnew.setXacNhanBcn(1);
			//System.out.println("yess");
		}
		System.out.println("xac nhan: "+request.getParameter("xacnhanbcn"));
		congvanService.updatePhanCong(pccvdnew, idphancong);
    	return "redirect:/banchunhiem";
    }
	@RequestMapping(value = "/chitietphancong/{idcongvanden}", method = RequestMethod.GET)
	public String ChiTietPhanCong(Model m, @PathVariable("idcongvanden") String idcongvanden, HttpServletRequest request) throws ParseException
	{
		int idcanbo =  (int) request.getSession().getAttribute("username");
		Canbo nguoiphancong =  userService.getTenCanBoById(idcanbo);
		List<Phancongcongvanden> listPhanCong = congvanService.getPhanCongbyId(idcongvanden, nguoiphancong.getTenCanBo());
		m.addAttribute("listPhanCong", listPhanCong);
		return "chitietphancong";
	}
	@RequestMapping(value = "/xemphancong/{idcongvanden}", method = RequestMethod.GET)
	public String XemPhanCong(Model m, @PathVariable("idcongvanden") String idcongvanden, HttpServletRequest request) throws ParseException
	{
		int idcanbo =  (int) request.getSession().getAttribute("username");
		List<Phancongcongvanden> listPhanCong = congvanService.getPhanCongbyId(idcongvanden);
		m.addAttribute("listPhanCong", listPhanCong);
		//hien thi ten va chuc vu ra jsp
        m.addAttribute("chucvu", userService.getChucVuByQuyen(idcanbo));
        m.addAttribute("tencanbo", userService.getTenCanBoById(idcanbo).getTenCanBo());
		return "xemphancong";
	}
	@RequestMapping(value = "/thanhtra", method = RequestMethod.GET)
	public String UserThanhTra(Model m, HttpServletRequest request)
	{
		if(request.getSession().getAttribute("username") != null){
			int idcanbo =  (int) request.getSession().getAttribute("username");
			//hien thi ten va chuc vu ra jsp
	        m.addAttribute("chucvu", userService.getChucVuByQuyen(idcanbo));
	        m.addAttribute("tencanbo", userService.getTenCanBoById(idcanbo).getTenCanBo());
			return "thanhtra";
		}
		else{
			return "chuadangnhap";
		}
		
	}
	@RequestMapping(value = "/bomon", method = RequestMethod.GET)
	public String UserBoMon(Model m, HttpServletRequest request)
	{
		if(request.getSession().getAttribute("username") != null){
			int idcanbo =  (int) request.getSession().getAttribute("username");
			Canbo cb =  userService.getTenCanBoById(idcanbo);
			m.addAttribute("idcanbo", idcanbo);
			m.addAttribute("idbomon", cb.getGiangvien().getBomon().getIdBoMon());
	    	List<PhancongcongvandenGiangvien> list1 = congvanService.layPhancongcongvandenGiangvien();
	    	List<PhancongcongvandenBomon> list2 = congvanService.layPhancongcongvandenBomon();
	        m.addAttribute("PhancongcongvandenGiangvien", list1);
	        m.addAttribute("PhancongcongvandenBomon", list2);
	      //hien thi ten va chuc vu ra jsp
	        m.addAttribute("chucvu", userService.getChucVuByQuyen(idcanbo));
	        m.addAttribute("tencanbo", userService.getTenCanBoById(idcanbo).getTenCanBo());
			return "bomon";
		}
		else{
			return "chuadangnhap";
		}
		
	}
    @RequestMapping(value = "/giangvien", method = RequestMethod.GET)
    public String getCongVanPhanCong(Model m, HttpServletRequest request)
    {
		if(request.getSession().getAttribute("username") != null){
			List<PhancongcongvandenGiangvien> list = congvanService.layPhancongcongvandenGiangvien();
			int idcanbo =  (int) request.getSession().getAttribute("username");
			m.addAttribute("idcanbo", idcanbo);
	        m.addAttribute("PhancongcongvandenGiangvien", list);
	      //hien thi ten va chuc vu ra jsp
	        m.addAttribute("chucvu", userService.getChucVuByQuyen(idcanbo));
	        m.addAttribute("tencanbo", userService.getTenCanBoById(idcanbo).getTenCanBo());
	    	return "giangvien";
		}
		else{
			return "chuadangnhap";
		}
    	
    }
    @RequestMapping(value = "/congvanden", method = RequestMethod.GET)
    public String getCongVanDen(Model m) {
    	List<Congvan> list = congvanService.getCongVan();
        m.addAttribute("listCongVan", list);
        return "congvanden";
    }
    @RequestMapping(value = "/congvandi", method = RequestMethod.GET)
    public String getCongVanDi(Model m) {
    	List<Congvan> list = congvanService.getCongVan();
        m.addAttribute("listCongVan", list);
        return "congvandi";
    }
    @RequestMapping(value = "/phancong", method = RequestMethod.GET)
    public String getPhanCong(Model m, HttpServletRequest request) {
		String username = (String) request.getSession().getAttribute("username");
		m.addAttribute("username", username);
    	List<Congvanden> list = congvanService.getCongVanDen();
        m.addAttribute("listCongVanDen", list);
        return "phancong";
    }
    @RequestMapping(value = "/themcongvanden", method = RequestMethod.POST)
    public String addCongVan(HttpServletRequest request, Model m) throws ParseException
    {
    	Congvan congvan = new Congvan();
    	Congvanden congvanden = new Congvanden();
    	congvan.setIdCongVan(request.getParameter("idcongvan"));
    	congvan.setTrichYeu(request.getParameter("trichyeu"));
    	congvan.setFileDinhKem(request.getParameter("filedinhkem"));
    	congvanService.addCongVan(congvan);
    	congvanden.setIdCongVanDen(request.getParameter("idcongvan"));
    	congvanden.setCongvan(congvan);
    	congvanden.setNgayNhan(new SimpleDateFormat("dd-MM-yyyy").parse(request.getParameter("ngaynhan")));
    	congvanden.setNoiGui(request.getParameter("noigui"));
    	congvanService.addCongVanDen(congvanden);
    	return "redirect:/thuky";
    }
    @RequestMapping(value = "/updatecongvanden", method = RequestMethod.POST)
    public String updateCongVan(@RequestParam("idcongvan") String idCongVan, HttpServletRequest request) throws ParseException
    {
    	Congvan newcv = new Congvan();
		newcv.setTrichYeu(request.getParameter("trichyeu"));
    	newcv.setFileDinhKem(request.getParameter("filedinhkem"));
    	Congvanden cvd = new Congvanden();			
		cvd.setNgayNhan(new SimpleDateFormat("dd-MM-yyyy").parse(request.getParameter("ngaynhan")));
    	cvd.setNoiGui(request.getParameter("noigui"));
    	newcv.setCongvanden(cvd);
    	congvanService.updateCongVan(newcv, idCongVan);
    	return "redirect:/thuky";
    }     
	@RequestMapping(value = "suacongvanden/{idcongvan}")
    public String editCongvan(@PathVariable("idcongvan") String idcongvan, Model m){
    	List<Congvan> list =  congvanService.getListCongvanById(idcongvan);
    	m.addAttribute("listCongVanForm", list);
    	return "suacongvanden";
    }
	@RequestMapping(value = "/test", method = RequestMethod.GET)
	public String Test(Model m)
	{
		return "test";
	}
	@RequestMapping(value = "QLKhoaCNTT/xuly", method = RequestMethod.POST)
	public String XuLy(Model m, HttpServletRequest request)
	{
		m.addAttribute("test", request.getParameter("number"));
		return "result";
	}
	//Xac nhan hoan tat phan cong
    @RequestMapping(value = "/xacnhanhoantat/{idphancong}")
    public String xacNhanHoanTat(@PathVariable("idphancong") int idphancong, HttpServletRequest request)
    {
    	Phancongcongvanden phancongcongvanden = new Phancongcongvanden();
    	phancongcongvanden.setXacNhanHoanTat(1);
    	congvanService.xacNhanHoanTat(phancongcongvanden, idphancong);
    	return "redirect:/giangvien";
    }
	//Bo Xac nhan hoan tat phan cong
    @RequestMapping(value = "/boxacnhan/{idphancong}")
    public String boXacNhan(@PathVariable("idphancong") int idphancong, HttpServletRequest request)
    {
    	Phancongcongvanden phancongcongvanden = new Phancongcongvanden();
    	phancongcongvanden.setXacNhanHoanTat(0);
    	congvanService.xacNhanHoanTat(phancongcongvanden, idphancong);
    	return "redirect:/giangvien";
    }
  //Xac nhan hoan tat phan cong bo mon
    @RequestMapping(value = "/xacnhanhoantatbomon/{idphancong}")
    public String xacNhanHoanTatBoMon(@PathVariable("idphancong") int idphancong, HttpServletRequest request)
    {
    	Phancongcongvanden phancongcongvanden = new Phancongcongvanden();
    	phancongcongvanden.setXacNhanHoanTat(1);
    	congvanService.xacNhanHoanTat(phancongcongvanden, idphancong);
    	return "redirect:/bomon";
    }
	//Bo Xac nhan hoan tat phan cong bo mon
    @RequestMapping(value = "/boxacnhanbomon/{idphancong}")
    public String boXacNhanBoMon(@PathVariable("idphancong") int idphancong, HttpServletRequest request)
    {
    	Phancongcongvanden phancongcongvanden = new Phancongcongvanden();
    	phancongcongvanden.setXacNhanHoanTat(0);
    	congvanService.xacNhanHoanTat(phancongcongvanden, idphancong);
    	return "redirect:/bomon";
    }
    //chi tiet cong van den
    @RequestMapping(value = "/chitietcongvandengiangvien/{idcongvanden}")
    public String xemChiTietCongVanDenGiangVien(@PathVariable("idcongvanden") String idcongvanden, HttpServletRequest request, Model m)
    {
    	List<Congvan> list = congvanService.getListCongvanById(idcongvanden);
		m.addAttribute("listChiTietCongVanDen", list);
    	return "chitietcongvandengiangvien";
    }
    //chi tiet cong van den
    @RequestMapping(value = "/chitietcongvandenbomon/{idcongvanden}")
    public String xemChiTietCongVanDenBoMon(@PathVariable("idcongvanden") String idcongvanden, HttpServletRequest request, Model m)
    {
    	List<Congvan> list = congvanService.getListCongvanById(idcongvanden);
		m.addAttribute("listChiTietCongVanDen", list);
    	return "chitietcongvandenbomon";
    }

}
